package org.itzheng.utils;

import org.jetbrains.annotations.NotNull;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2020-4-15.
 */
public class ViewModelUtils {
    public static <T extends ViewModel> T getViewModel(FragmentActivity activity, Class<T> clazz) {
//        return ViewModelProviders.of(activity, new SavedStateViewModelFactory(activity.getApplication(), activity)).get(clazz);
//        return new ViewModelProvider(activity).get(clazz);
//        ViewModelProvider(                activity,                AndroidViewModelFactory.getInstance(application, name)        ).get(ViewModel.class)

//        return new ViewModelProvider(activity, ViewModelProvider.AndroidViewModelFactory.getInstance(activity.getApplication())).get(clazz);
        return new ViewModelProvider(activity).get(clazz);
    }

    public static <T extends ViewModel> T getViewModel(@NotNull Fragment fragment, @NotNull Class<T> clazz) {
//        return new ViewModelProvider(fragment, ViewModelProvider.AndroidViewModelFactory.getInstance(fragment.getActivity().getApplication())).get(clazz);
        return new ViewModelProvider(fragment).get(clazz);
    }
}
